package com.calendar;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * https://blog.csdn.net/yx0628/article/details/79317440
 */
public class Canlendar {
    /**
     * 时间 时间设置有点奇怪，设置的时候要将当前一个月减掉一个月，获取的时候又要加上一个月
     */
    public void currentDate(){
        // 使用默认时区和语言环境获得一个日历
        Calendar cal = Calendar.getInstance();
        cal.set(2020,12-1,20);
        System.out.println(cal.getTime());
        // 赋值时年月日时分秒常用的6个值，注意月份下标从0开始，所以取月份要+1
        System.out.println(cal.get(Calendar.DATE));
        System.out.println("年:" + cal.get(Calendar.YEAR));
        System.out.println("月:" + (cal.get(Calendar.MONTH)+1));//月份的下标从0 开始
        System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH));
        System.out.println("时:" + cal.get(Calendar.HOUR_OF_DAY));
        System.out.println("分:" + cal.get(Calendar.MINUTE));
        System.out.println("秒:" + cal.get(Calendar.SECOND));
        Integer week = null; ;
        if(cal.get(Calendar.DAY_OF_WEEK) == 1){
            week = 7;
        }
        else{
            week = cal.get(Calendar.DAY_OF_WEEK)-1;
        }
        System.out.println("周几："+(week));//国外的星期天是一个星期的开始

    }

    /**
     * 设置时间
     */
    public void setDate(){
        Calendar calendar = Calendar.getInstance();
        //一次性设置
//        calendar.set(2018,1,18,23,23,23);
//        System.out.println(calendar.getTime());
        //单独设置
        calendar.set(Calendar.YEAR,2020);
        calendar.set(Calendar.MONTH,10);
        calendar.set(Calendar.DATE,20);
//        calendar.set(Calendar.HOUR_OF_DAY,6);
//        calendar.set(Calendar.MINUTE,6);
//        calendar.set(Calendar.MILLISECOND,6);
        System.out.println(calendar.getTime());
        System.out.println("周几:"+calendar.get(Calendar.DAY_OF_WEEK));


    }

    /**
     * 日历计算
     */
    public void compute(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(2020,12,31,23,59,59);
        calendar.add(Calendar.SECOND,1);
        System.out.println(calendar.getTime());
        System.out.println(calendar.getTime().getTime());
        System.out.println(calendar.getTimeInMillis());
    }

    /**
     * 获取周几
     * @param date
     * @return
     */
    public int  getDayofWeek(String date){
        String[] split = date.split("-");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.parseInt( split[0]),Integer.parseInt( split[1])-1,Integer.parseInt( split[2]));
        Integer day = calendar.get(Calendar.DAY_OF_WEEK);
        if(day == 1){
            day = 7;
        }
        else{
            day -= 1;
        }
        return day;

    }
    public void week(){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, 1);
        System.out.println(calendar.getTime());
        calendar.add(Calendar.DATE,7);
        System.out.println(calendar.getTime());
    }
    public static void main(String[] args) {
        Canlendar canlendar  = new Canlendar();
       // canlendar.setDate();
//        canlendar.compute();
//        canlendar.computeDate("2020-10-20");
//        canlendar.currentDate();
//
//        TimeZone timeZone = TimeZone.getDefault();
//        System.out.println(timeZone.getDisplayName());
//        System.out.println(  canlendar.getDayofWeek("2020-10-17"));
        canlendar.week();
    }
}
